package com.songcha.module_task.controller

import android.content.Context
import com.alibaba.android.arouter.facade.annotation.Route
import com.songcha.library_base.handler.ApiObservableHandler
import com.songcha.library_base.interfaces.IApiResult
import com.songcha.library_business.provider.IDailyTaskProvider
import com.songcha.library_business.provider.ILogoutProvider
import com.songcha.library_business.provider.IUnRegisterProvider
import com.songcha.library_business.proxy.userinfo.UserInfoManager
import com.songcha.library_common.BuildConfig
import com.songcha.library_common.util.LogUtil
import com.songcha.library_common.util.ToastUtil
import com.songcha.module_task.api.TaskApiManager
import com.songcha.module_task.bean.DailyTaskBean
import okhttp3.RequestBody.Companion.toRequestBody
import org.json.JSONObject

@Deprecated("no use")
@Route(path="/task/DailyTaskController")
class DailyTaskController: IDailyTaskProvider, ILogoutProvider,IUnRegisterProvider {

    private var mTaskList=mutableListOf<DailyTaskBean.DataBean>()

    override fun handleDailyTask(type: IDailyTaskProvider.DailyType, arg: Int) {
        LogUtil.log("handleDailyTask",type,arg)
        if(!UserInfoManager.checkLogin()) return
        //if(mTaskList.size==0) return

        if(mTaskList.size==0){
            val json= JSONObject().apply {
                if(UserInfoManager.isLogin && UserInfoManager.userInfo!=null)
                    put("token",UserInfoManager.loginToken)
            }
            ApiObservableHandler.handle(TaskApiManager.api.getDailyTask(json.toString().toRequestBody()),object :IApiResult<DailyTaskBean>{
                override fun onSuccess(bean: DailyTaskBean) {
                    mTaskList=bean.data
                    checkDailyTask(type,arg)
                }
            })
        }else{
            checkDailyTask(type,arg)
        }
    }

    private fun checkDailyTask(type: IDailyTaskProvider.DailyType,arg:Int){
        if(type==IDailyTaskProvider.DailyType.READ_CHAPTER){
            val idx=findReadChapterTaskIdx()
            if(idx==-1) return
            val task=mTaskList[idx]
            if(arg<task.norm) return
            completeTask(idx)
        }else if(type==IDailyTaskProvider.DailyType.READ_TIME){
            val idx=findReadTimeTaskIdx()
            if(idx==-1) return
            val task=mTaskList[idx]
            if(arg<task.norm*60) return
            if(BuildConfig.DEBUG)
                ToastUtil.show("完成阅读时间超60分钟任务")
            completeTask(idx)
        }else if(type==IDailyTaskProvider.DailyType.WRITE_COMMENT){
            val idx=findWriteCommentTaskIdx()
            if(idx==-1) return
            val task=mTaskList[idx]
            if(arg<task.norm) return
            completeTask(idx)
        }else if(type==IDailyTaskProvider.DailyType.WATCH_REWARD_ADVERT){
            val idx=findWatchRewardAdvertTaskIdx()
            if(idx==-1) return
            val task=mTaskList[idx]
            if(arg<task.norm) return
            completeTask(idx)
        }
    }

    private fun completeTask(idx:Int){
      /*  if(idx==-1) return
        if(mTaskList.size<idx) return
        val task=mTaskList[idx]
        if(task.isAccomplish) return
        ApiObservableHandler.handle(TaskApiManager.api.completeTask(task.id),object :IApiResult<BaseIntDataBean>{
            override fun onSuccess(bean: BaseIntDataBean) {
                mTaskList[idx].isAccomplish=true
                BookTodayObtainGoldCountDaoManager.addTodayObtainGoldCount(bean.data)
                EventBus.getDefault().postSticky(DailyTaskCompleteEvent())
            }
        },false)*/
    }

    private fun findReadChapterTaskIdx():Int{
        for(i in 0 until mTaskList.size){
            val item=mTaskList[i]
            if(item.taskType==1) return i
        }
        return -1
    }

    private fun findReadTimeTaskIdx():Int{
        for(i in 0 until mTaskList.size){
            val item=mTaskList[i]
            if(item.taskType==2) return i
        }
        return -1
    }

    private fun findWriteCommentTaskIdx():Int{
        for(i in 0 until mTaskList.size){
            val item=mTaskList[i]
            if(item.taskType==3) return i
        }
        return -1
    }

    private fun findWatchRewardAdvertTaskIdx():Int{
        for(i in 0 until mTaskList.size){
            val item=mTaskList[i]
            if(item.taskType==4) return i
        }
        return -1
    }


    fun setTaskList(taskList: MutableList<DailyTaskBean.DataBean>){
        mTaskList=taskList
    }

    //退出登录时要清除task
    override fun onLogout() {
        mTaskList.clear()
    }

    override fun onUnRegister() {
        mTaskList.clear()
    }

    override fun init(context: Context?) {

    }
}